package com.sh.sales.happysales.ui.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.sh.sales.happysales.R;
import com.sh.sales.happysales.app.AppCondig;
import com.sh.sales.happysales.app.AppContext;
import com.sh.sales.happysales.service.WechatInLunchObserable;
import com.sh.sales.happysales.service.WechatInLunchObsever;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity implements WechatInLunchObsever {

    @BindView(R.id.content)
    TextView content;
    @BindView(R.id.lunch)
    Button lunch;
    @BindView(R.id.copy)
    Button copy;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WechatInLunchObserable.getInstance().registerObserver(this);
        context = this;
        ButterKnife.bind(this);
    }


    @Override
    public void testShow(String rep) {
        content.setText(rep);
    }

    @Override
    protected void onDestroy() {
        WechatInLunchObserable.getInstance().unregisterObserver(this);
        super.onDestroy();
    }

    @OnClick({R.id.lunch, R.id.copy})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lunch:
                IWXAPI mApi = AppContext.getIwxapi();
                mApi.registerApp(AppCondig.WX_APPID);
                if (mApi != null && mApi.isWXAppInstalled()) {
                    SendAuth.Req req = new SendAuth.Req();
                    req.scope = "snsapi_userinfo";
                    req.state = "wechat_sdk_demo_test_neng";
                    mApi.sendReq(req);
                } else{
                    Toast.makeText(this, "用户未安装微信", Toast.LENGTH_SHORT).show();}
                break;
            case R.id.copy:
                // 从API11开始android推荐使用android.content.ClipboardManager
                // 为了兼容低版本我们这里使用旧版的android.text.ClipboardManager，虽然提示deprecated，但不影响使用。
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                // 将文本内容放到系统剪贴板里。
                cm.setText(content.getText());
                Toast.makeText(this, "已经复制到粘贴板", Toast.LENGTH_LONG).show();
                break;
        }
    }
}
